package com.qeegoo.b2bautozityp.moudle.good.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qeegoo.b2bautozityp.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 王少岩 在 2017/9/21 创建了它
 */

public class BrandAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<String> mLetter;
    private JSONObject mBrandList;

    public BrandAdapter(Context context, ArrayList<String> letter, JSONObject brandList) {
        mContext = context;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mLetter = letter;
        mBrandList = brandList;
    }

    @Override
    public int getGroupCount() {
        return mLetter.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int length = getChildrenArray(groupPosition).length();
        return length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mLetter.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        Object o = null;
        try {
            o = getChildrenArray(groupPosition).get(childPosition);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return o;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        TextView textView;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_goods_brand_header, null);
            convertView.setOnClickListener(v -> {});
            textView = (TextView) convertView.findViewById(R.id.tv_letter);
            convertView.setTag(textView);
        } else {
            textView = (TextView) convertView.getTag();
        }

        String title = (String) getGroup(groupPosition);
        textView.setText(title);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_goods_brand_item, null);
        }

        JSONObject json = (JSONObject) getChild(groupPosition, childPosition);
        convertView.setTag(json);
        ImageView ivLogo = (ImageView) convertView.findViewById(R.id.iv_logo);
        TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
        try {
            tvName.setText(json.getString("name"));
            Glide.with(mContext).load(json.getString("logo")).into(ivLogo);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    private JSONArray getChildrenArray(int groupPosition) {
        String key = mLetter.get(groupPosition);
        try {
            JSONArray array1 = mBrandList.getJSONObject("brandList").getJSONArray(key);
            return array1;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
